//
//  MineViewController.m
//  XIWenLeJian
//
//  Created by lanou3g on 15/11/11.
//  Copyright © 2015年 蓝足. All rights reserved.
//

#import "MineViewController.h"
#import "MineTableViewCell.h"
#import "LoginViewController.h"
#import "CollectTableViewController.h"
#import "UserInfo.h"
#import "SDWebImageManager.h"
#import "UIImageView+WebCache.h"

#define kCaches NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject

@interface MineViewController ()


@end

@implementation MineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableView registerClass:[MineTableViewCell class] forCellReuseIdentifier:@"cell"];
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 6;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    if (indexPath.row == 0) {
        cell.label.text = @"我的账户";
        cell.mineImageView.image = [UIImage imageNamed:@"iconfont-taogongzi"];
    }else if (indexPath.row == 1){
        cell.label.text = @"管理我的收藏";
        cell.mineImageView.image = [UIImage imageNamed:@"iconfont-guanlizhongxin"];
    }else if (indexPath.row == 2){
        cell.label.text = @"清除缓存";
        cell.mineImageView.image = [UIImage imageNamed:@"iconfont-huodongguanli"];
    }else if (indexPath.row == 3 && [UserInfo shareUserInfo].isNight == YES){
        cell.label.text = @"夜间模式";
        cell.mineImageView.image = [UIImage imageNamed:@"iconfont-yejianmoshi"];
    }else if (indexPath.row == 3 && [UserInfo shareUserInfo].isNight == NO){
        cell.label.text = @"日间模式";
        cell.mineImageView.image = [UIImage imageNamed:@"iconfont-qingbaitian"];
    }else if (indexPath.row == 4){
        cell.label.text = @"关于我";
        cell.mineImageView.image = [UIImage imageNamed:@"iconfont-guanyu"];
    }else{
        cell.label.text = @"设置";
        cell.mineImageView.image = [UIImage imageNamed:@"iconfont-shezhi"];
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        LoginViewController *loginVC = [[LoginViewController alloc]init];
        [self.navigationController pushViewController:loginVC animated:YES];
    }else if (indexPath.row == 1){
        if ([UserInfo shareUserInfo].isLogin == NO) {
            UIAlertView *loseLogin = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您当前尚未登陆，请先登录！" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles: nil, nil];
            [loseLogin show];
            LoginViewController *loginVC = [[LoginViewController alloc]init];
            [self.navigationController pushViewController:loginVC animated:YES];
        }else{
            CollectTableViewController *collectVC = [[CollectTableViewController alloc] init];
            [self.navigationController pushViewController:collectVC animated:YES];
        }
    }else if (indexPath.row == 2){
        UIAlertView * alertView = [[ UIAlertView alloc ] initWithTitle : @" 提示 " message :[NSString stringWithFormat:@"已为你清理缓冲%.2fM!",[self folderSizeAtPath:kCaches]] delegate : nil cancelButtonTitle : @" 确定 " otherButtonTitles : nil ];
        
        [alertView show ];
        [self clearCache:kCaches];

    }else if (indexPath.row == 3 && [UserInfo shareUserInfo].isNight == NO){
        [UIApplication sharedApplication].keyWindow.alpha = 0.7;
        [UserInfo shareUserInfo].isNight = YES;
        [self.tableView reloadData];
    }else if (indexPath.row == 3 && [UserInfo shareUserInfo].isNight == YES){
        [UIApplication sharedApplication].keyWindow.alpha = 1.0;
        [UserInfo shareUserInfo].isNight = NO;
        [self.tableView reloadData];
    }
    else if (indexPath.row == 4){
        UIAlertView *description = [[UIAlertView alloc]initWithTitle:@"喜闻乐贱" message:@"本应用所有内容收集整理均来自互联网，访问者可将本网站内容用于学习欣赏，以及其他非商业用途，并同时遵循本网站的合法权利,谢谢您的使用。\n\n版本V0.01" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles: nil, nil];
        [description show];
    }else{
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@" 提示 " message:@"你想设置什么呢, 先让我准备一下, 暂不支持！！！" delegate:nil cancelButtonTitle:@" 确定 "  otherButtonTitles:nil, nil];
        [alertView show];
    }

}

// 缓存处理!!!!
- (float)folderSizeAtPath:(NSString *)path{
    NSFileManager *fileManager=[NSFileManager defaultManager];
    float folderSize;
    if ([fileManager fileExistsAtPath:path]) {
        NSArray *childerFiles=[fileManager subpathsAtPath:path];
        for (NSString *fileName in childerFiles) {
            NSString *absolutePath=[path stringByAppendingPathComponent:fileName];
            folderSize += [self fileSizeAtPath:absolutePath];
        }
        //SDWebImage框架自身计算缓存的实现
        folderSize+=[[SDImageCache sharedImageCache] getSize]/1024.0/1024.0;
        return folderSize;
    }
    return 0;
}

- (float)fileSizeAtPath:(NSString *)path{
    NSFileManager *fileManager=[NSFileManager defaultManager];
    if([fileManager fileExistsAtPath:path]){
        long long size=[fileManager attributesOfItemAtPath:path error:nil].fileSize;
        return size/1024.0/1024.0;
    }
    return 0;
}

- (void)clearCache:(NSString *)path{
    NSFileManager *fileManager=[NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path]) {
        NSArray *childerFiles=[fileManager subpathsAtPath:path];
        for (NSString *fileName in childerFiles) {
            //如有需要，加入条件，过滤掉不想删除的文件
            NSString *absolutePath=[path stringByAppendingPathComponent:fileName];
            [fileManager removeItemAtPath:absolutePath error:nil];
        }
    }
    [[SDImageCache sharedImageCache] cleanDisk];
}


@end
